package com.cn.aam.checaiduo.ui.me;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestV2Listener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.bean.base.NormalResponseB;
import com.cn.aam.checaiduo.bean.me.VoucherB;
import com.cn.aam.checaiduo.ui.homepage.adapter.MyFragmentPageAdapter;
import com.cn.aam.checaiduo.ui.me.fragment.VoucherFragment;
import com.cn.aam.checaiduo.ui.me.helper.MeHelper;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * monker on 2017/5/25.
 * 理财券
 */

public class FinancialVoucherActivity extends BaseActivity {

    @BindView(R.id.voucher_tl_bar)
    SegmentTabLayout voucherTlBar;
    @BindView(R.id.voucher_viewpager)
    ViewPager mViewPager;

    private String[] mTitles = {"代金券", "加息券"};


    public static Intent newInstance(Context context) {
        Intent intent = new Intent(context, FinancialVoucherActivity.class);
        return intent;
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_financia_voucher;
    }

    @Override
    protected void initView() {
        super.initView();
        ToolBarManager.with(this, WMContent).setNavigationIcon(R.drawable.ic_nav_arrow_white)
                .setTitle(getString(R.string.voucher));

        voucherTlBar.setTabData(mTitles);

        RetrofitHome.getInstance()
                .getInternalApi()
                .queryCoupon(AppData.getToken())
                .compose(RxSchedulers.<NormalResponseB<VoucherB>>toMain())
                .subscribe(new OnRequestV2Listener<VoucherB>(this) {
                    @Override
                    public void onNext(NormalResponseB<VoucherB> value) {
                        super.onNext(value);
                        if (value.getIsLogged() != Const.STATE_LOGIN) {
                            goLogin();
                            return;
                        }
                        initData(value.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        initData(null);
                    }
                });

    }


    private void initData(VoucherB bean) {
        List<Fragment> fragments = new ArrayList<>();
        MeHelper.sortList(bean);
        fragments.add(VoucherFragment.newInstance(bean, VoucherFragment.CASH_COUPON));
        fragments.add(VoucherFragment.newInstance(bean, VoucherFragment.RATE_COUPON));

        mViewPager.setAdapter(new MyFragmentPageAdapter(getSupportFragmentManager(), fragments));

        voucherTlBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                voucherTlBar.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

}
